package com.thinkin.services.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.thinkin.allDo.SysDao;
import com.thinkin.fondation.po.ReSku;
import com.thinkin.fondation.po.Sku;
import com.thinkin.fondation.po.SkuType;
import com.thinkin.util.JsonHelper;

public class BaseService {

	public static String doSave(List<String> keys, List<Object> values,
			String clazz) throws Exception {
		String result = "";
		try {
			StringBuffer stringBuffer = new StringBuffer();
			stringBuffer.append("INSERT INTO  " + clazz);
			stringBuffer.append("(");
			for (int key = 0; keys != null && keys.size() > 0
					&& key < keys.size(); key++) {
				if (key == 0) {
					stringBuffer.append("" + keys.get(key).toString() + "");
				} else {
					stringBuffer.append("," + keys.get(key).toString() + "");

				}
			}
			// stringBuffer.append(",CREATEDATE");
			stringBuffer.append(")");

			stringBuffer.append("  VALUES(");
			for (int param = 0; keys != null && keys.size() > 0
					&& param < keys.size(); param++) {
				if (param == 0) {
					stringBuffer.append("?");
				} else {
					stringBuffer.append(",?");

				}
			}
			// stringBuffer.append(",?");
			stringBuffer.append(" )");
			result = SysDao.getCommonServer().doSave(stringBuffer.toString(),
					values.toArray(), true);
			// values.add(ExtendDate.getTimestamp(ExtendDate.getYMD_h_m_s(new
			// Date())));
		} catch (Exception e) {
			throw e;
		}
		return result.replaceAll("[\\n\\r\\t]", "");

	}

	public static String doSave(List<String> keys, List<Object> values,
			String clazz, String datefield) throws Exception {
		String result = "";
		String str_Todate = "to_date(?,'YYYY-MM-DD HH24:MI:SS')";
		try {
			StringBuffer stringBuffer = new StringBuffer();
			stringBuffer.append("INSERT INTO  " + clazz);
			stringBuffer.append("(");
			for (int key = 0; keys != null && keys.size() > 0
					&& key < keys.size(); key++) {
				if (key == 0) {
					stringBuffer.append("" + keys.get(key).toString() + "");
				} else {
					stringBuffer.append("," + keys.get(key).toString() + "");

				}
			}
			stringBuffer.append(")");

			stringBuffer.append("  VALUES(");
			for (int param = 0; keys != null && keys.size() > 0
					&& param < keys.size(); param++) {

				if (param == 0) {
					if (datefield.contains(keys.get(param))) {
						stringBuffer.append(str_Todate);
					} else {
						stringBuffer.append("?");
					}
				} else {
					if (datefield.contains(keys.get(param))) {
						stringBuffer.append("," + str_Todate);
					} else {
						stringBuffer.append(",?");
					}
				}
			}
			// stringBuffer.append(",?");
			stringBuffer.append(" )");
			result = SysDao.getCommonServer().doSave(stringBuffer.toString(),
					values.toArray(), true);
		} catch (Exception e) {
			throw e;
		}
		return result.replaceAll("[\\n\\r\\t]", "");

	}

	public static String doValidateEdit(List<String> keys, List<Object> values,
			String clazz) {
		int keyindex = 0;
		Object keyidValue = "";
		String result = "{}";
		for (int key = 0; keys != null && keys.size() > 0 && key < keys.size(); key++) {
			if (keys.get(key).equals("keyid")) {
				keyindex = key;
				break;
			}
		}
		keyidValue = values.get(keyindex);
		keys.remove(keyindex);
		values.remove(keyindex);
		// Object[] objs = values.toArray();

		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT  *   FROM " + clazz + " WHERE 1=1  ");
		for (int key = 0; keys != null && keys.size() > 0 && key < keys.size(); key++) {

			stringBuffer.append("  AND " + keys.get(key).toString() + "='"
					+ values.get(key) + "'");

		}
		stringBuffer.append("  AND keyid!='" + keyidValue + "' ");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		result = JsonHelper.toJSONString(maps);
		return result;
	}

	public static String doValidateAdd(List<String> keys, List<Object> values,
			String clazz) {
		int keyindex = 0;

		String result = "{}";
		// for (int key = 0; keys != null && keys.size() > 0 && key <
		// keys.size(); key++) {
		// if (keys.get(key).equals("keyid")) {
		// keyindex = key;
		// break;
		// }
		// }
		// keys.remove(keyindex);
		// values.remove(keyindex);
		Object[] objs = values.toArray();

		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT  *   FROM    " + clazz + " WHERE 1=1  ");
		for (int key = 0; keys != null && keys.size() > 0 && key < keys.size(); key++) {

			stringBuffer.append("  AND " + keys.get(key).toString() + "='"
					+ values.get(key) + "'");

		}

		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		result = JsonHelper.toJSONString(maps);
		return result;
	}

	public static String doEdit(List<String> keys, List<Object> values,
			String clazz) {
		String result = "";
		int keyindex = 0;
		Object keyidValue = "";
		for (int key = 0; keys != null && keys.size() > 0 && key < keys.size(); key++) {
			if (keys.get(key).equals("keyid")) {
				keyindex = key;
				break;
			}
		}
		// values.add(ExtendDate.getTimestamp(ExtendDate.getYMD_h_m_s(new
		// Date())));
		keyidValue = values.get(keyindex);
		keys.remove(keyindex);
		values.remove(keyindex);
		Object[] objs = values.toArray();
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("UPDATE " + clazz + " SET  ");
		for (int key = 0; keys != null && keys.size() > 0 && key < keys.size(); key++) {
			if (key == 0) {
				stringBuffer.append("  " + keys.get(key).toString() + "=?");
			} else {
				stringBuffer.append("," + keys.get(key).toString() + "=?");
			}
		}
		// stringBuffer.append(",MODIFYDATE=?");
		stringBuffer.append(" WHERE keyid='" + keyidValue + "'");
		try {
			result = SysDao.getCommonServer().doEdit(stringBuffer.toString(),
					objs, true);
		} catch (Exception e) {
			result = e.getMessage();
		}
		return result.replaceAll("[\\n\\r\\t]", "");
	}

	public static String doEdit(List<String> keys, List<Object> values,
			String clazz, String datefield) throws Exception {
		String result = "";
		int keyindex = 0;
		Object keyidValue = "";
		String str_Todate = "to_date(?,'YYYY-MM-DD HH24:MI:SS')";
		for (int key = 0; keys != null && keys.size() > 0 && key < keys.size(); key++) {
			if (keys.get(key).equals("keyid")) {
				keyindex = key;
				break;
			}
		}
		// values.add(ExtendDate.getTimestamp(ExtendDate.getYMD_h_m_s(new
		// Date())));
		keyidValue = values.get(keyindex);
		keys.remove(keyindex);
		values.remove(keyindex);
		Object[] objs = values.toArray();
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("UPDATE " + clazz + " SET  ");
		for (int key = 0; keys != null && keys.size() > 0 && key < keys.size(); key++) {
			if (key == 0) {
				if (datefield.contains(keys.get(key))) {
					stringBuffer.append("  " + keys.get(key).toString() + "="
							+ str_Todate);
				} else {
					stringBuffer.append("  " + keys.get(key).toString() + "=?");
				}
			} else {
				if (datefield.contains(keys.get(key))) {
					stringBuffer.append("," + keys.get(key).toString() + "="
							+ str_Todate);
				} else {
					stringBuffer.append("," + keys.get(key).toString() + "=?");
				}
			}
		}
		// stringBuffer.append(",MODIFYDATE=?");
		stringBuffer.append(" WHERE keyid='" + keyidValue + "'");
		try {
			result = SysDao.getCommonServer().doEdit(stringBuffer.toString(),
					objs, true);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	public static String doQuery(List<String> keys, Object[] values,
			String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1");

		for (int value = 0; values != null && values.length > 0
				&& value < values.length; value++) {
			int index = value;

			if (StringUtils.isNotBlank(values[index].toString())) {

				stringBuffer.append(" AND " + keys.get(value) + "='"
						+ values[index] + "' ");
			}
		}
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		result = JsonHelper.toJSONString(maps);
		return result;
	}

	public static String skuQuery(List<String> keys, Object[] values,
			String viewTree, String clazz) {
		String result = "{}";
		List<ReSku> reskus = new ArrayList<ReSku>();
		// 1. 查询 商品类型
		if (StringUtils.isNotBlank(viewTree) && "T".equals(viewTree)) {
			StringBuffer strSkutype = new StringBuffer();
			strSkutype.append("SELECT *  FROM E_SKUTYPE");

			List<SkuType> skuTypeLs = SysDao.getSkuTypeDao().findQuery(
					strSkutype.toString(), true);

			for (int skutype = 0; skuTypeLs != null && skuTypeLs.size() > 0
					&& skutype < skuTypeLs.size(); skutype++) {
				SkuType skut = skuTypeLs.get(skutype);
				ReSku resku = new ReSku();
				resku.setSkutype(skut.getSkutype());
				resku.setParentid(skut.getParentcd());
				resku.setNodeid(skut.getClasscd());
				reskus.add(resku);
				// 2 根据商品类型 查询 商品
				StringBuffer strSku = new StringBuffer();
				strSku.append("SELECT *  FROM E_SKU ");
				strSku.append(" WHERE 1=1 ");
				strSku.append(" AND skutypekey='" + skut.getKeyid() + "'");
				for (int value = 0; values != null && values.length > 0
						&& value < values.length; value++) {
					int index = value;

					if (StringUtils.isNotBlank(values[index].toString())) {

						strSku.append(" AND " + keys.get(value) + "='"
								+ values[index] + "' ");
					}
				}
				List<Sku> skus = SysDao.getSkuDao().findQuery(
						strSku.toString(), true);
				if (skus != null && skus.size() > 0) {
					int node = 1;
					for (Sku sku : skus) {

						ReSku rs = new ReSku();
						rs.setKeyid(sku.getKeyid());
						rs.setParentid(skut.getClasscd());
						rs.setNodeid(skut.getClasscd() + "0" + node);
						rs.setBrandname(sku.getBrandname());
						rs.setSkubarcode(sku.getSkubarcode());
						rs.setSkucode(sku.getSkucode());
						rs.setSkucolor(sku.getSkucolor());
						rs.setSkumodel(sku.getSkumodel());
						rs.setSkuname(sku.getSkuname());
						rs.setSkuspec(sku.getSkuspec());
						rs.setSkutype(skut.getSkutype());
						rs.setStateid(sku.getStateid());
						rs.setPicpath(sku.getPicpath());
						rs.setQuantityunit(sku.getQuantityunit());
						rs.setPieceunit(sku.getPieceunit());
						rs.setVolumeunit(sku.getVolumeunit());
						rs.setWeightunit(sku.getWeightunit());
						rs.setSizeunit(sku.getSizeunit());
						rs.setLength(sku.getLength());
						rs.setWidth(sku.getWidth());
						rs.setHigh(sku.getHigh());
						reskus.add(rs);
						node++;
					}
				}

			}
		} else {
			StringBuffer strSku = new StringBuffer();
			strSku.append("SELECT *  FROM E_SKU ");
			strSku.append(" WHERE 1=1 ");
			for (int value = 0; values != null && values.length > 0
					&& value < values.length; value++) {
				int index = value;

				if (StringUtils.isNotBlank(values[index].toString())) {

					strSku.append(" AND " + keys.get(value) + "='"
							+ values[index] + "' ");
				}
			}
			List<Sku> skus = SysDao.getSkuDao().findQuery(strSku.toString(),
					true);
			if (skus != null && skus.size() > 0) {
				for (Sku sku : skus) {

					ReSku rs = new ReSku();
					rs.setKeyid(sku.getKeyid());
					rs.setParentid("");
					rs.setNodeid("");
					rs.setBrandname(sku.getBrandname());
					rs.setSkubarcode(sku.getSkubarcode());
					rs.setSkucode(sku.getSkucode());
					rs.setSkucolor(sku.getSkucolor());
					rs.setSkumodel(sku.getSkumodel());
					rs.setSkuname(sku.getSkuname());
					rs.setSkuspec(sku.getSkuspec());
					rs.setSkutype(sku.getSkutype());
					rs.setStateid(sku.getStateid());
					rs.setPicpath(sku.getPicpath());
					rs.setQuantityunit(sku.getQuantityunit());
					rs.setPieceunit(sku.getPieceunit());
					rs.setVolumeunit(sku.getVolumeunit());
					rs.setWeightunit(sku.getWeightunit());
					rs.setSizeunit(sku.getSizeunit());
					rs.setLength(sku.getLength());
					rs.setWidth(sku.getWidth());
					rs.setHigh(sku.getHigh());
					reskus.add(rs);
				}
			}
		}

		// StringBuffer stringBuffer = new StringBuffer();
		// stringBuffer
		// .append("SELECT sku.*,skut.classc as nodeid,skut.parentcd as parentid  FROM  ");
		// stringBuffer
		// .append(clazz
		// +
		// "  sku left join e_skutype skut on sku.skutypekey=skut.keyid WHERE 1=1");
		// for (int value = 0; values != null && values.length > 0
		// && value < values.length; value++) {
		// int index = value;
		//
		// if (StringUtils.isNotBlank(values[index].toString())) {
		//
		// stringBuffer.append(" AND sku." + keys.get(value) + "='"
		// + values[index] + "' ");
		// }
		// }
		// StringBuffer stringBuffer = new StringBuffer();
		// stringBuffer.append("SELECT");
		// stringBuffer
		// .append("  e_skutype.skutype, e_skutype.detail,e_skutype.parentcd, e_skutype.classcd,");
		// stringBuffer
		// .append("   '' as keyid,'' as skuname,'' as skucode, '' as skumodel, ");
		// stringBuffer
		// .append("   '' as skuspec,'' as skucolor,'' as skutype,'' as brandname,");
		// stringBuffer.append("   '' as skubarcode,'' as stateid");
		// stringBuffer.append(" FROM e_skutype ");
		// stringBuffer.append(" UNION ALL ");
		// stringBuffer.append("SELECT  ");
		// stringBuffer
		// .append("  e_skutype.skutype,  e_skutype.detail, e_skutype.classcd as parentcd,'' as classcd,");
		// stringBuffer
		// .append(" e_sku.keyid, e_sku.skuname,  e_sku.skucode, e_sku.skumodel,");
		// stringBuffer
		// .append(" e_sku.skuspec,e_sku.skucolor,e_sku.skutype,e_sku.brandname,");
		// stringBuffer.append(" e_sku.skubarcode, e_sku.stateid");
		// stringBuffer.append("  FROM e_skutype");
		// stringBuffer
		// .append(" join e_sku on e_skutype.keyid = e_sku.skutypekey");
		// stringBuffer.append(" WHERE 1=1");
		// for (int value = 0; values != null && values.length > 0
		// && value < values.length; value++) {
		// int index = value;
		// if (StringUtils.isNotBlank(values[index].toString())) {
		// stringBuffer.append(" AND e_sku." + keys.get(value) + "='"
		// + values[index] + "' ");
		// }
		// }
		// stringBuffer.append(" ORDER BY parentcd");
		//
		// List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
		// stringBuffer.toString(), true);
		result = JsonHelper.toJSONString(reskus);
		return result;

	}

	public static String baseEcodelibClass(String clazz) {

		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz
				+ " WHERE 1=1 order by codeclassno asc");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		List<Map<String, Object>> mapLs = new ArrayList<Map<String, Object>>();
		for (int key = 0; maps != null && maps.size() > 0 && key < maps.size(); key++) {
			Map<String, Object> mapLocal = maps.get(key);
			Map<String, Object> map = new HashMap<String, Object>();
			String keyid = (String) mapLocal.get("keyid");
			String text = mapLocal.get("codeclassno").toString() + " "
					+ mapLocal.get("codeclassname").toString();
			String parentid = "0";
			String valuz = mapLocal.get("codeclass").toString();
			map.put("keyid", keyid);
			map.put("text", text);
			map.put("parentid", parentid);
			map.put("valuz", valuz);

			mapLs.add(map);
		}
		result = JsonHelper.toJSONString(mapLs);
		return result;
	}

	public static String doDelete(List<String> keys, Object[] values,
			String clazz) {
		String result = "";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("DELETE FROM " + clazz + " WHERE 1=1");
		stringBuffer.append(" AND " + keys.get(0) + "=?");
		try {
			result = SysDao.getCommonServer().doDelete(stringBuffer.toString(),
					values, true);
			result = Integer.parseInt(result) > 0 ? "" : "删除失败";
		} catch (Exception e) {
			result = "{\"Error\":\"" + e.getMessage() + "\"}";
		}
		return result.replaceAll("[\\n\\r\\t]", "");
	}

	public static String doDeletePFK(String pkeyid, String tablepname,
			List<String> keys, Object[] values, String tablefname) {
		String result = "";
		StringBuffer pBuffer = new StringBuffer();
		pBuffer.append("DELETE FROM " + tablepname + " WHERE 1=1");
		pBuffer.append(" AND keyid ='" + pkeyid + "'");
		// 删除主表
		SysDao.getCommonServer().doDelete(pBuffer.toString(), true);
		// 删除子表
		StringBuffer fBuffer = new StringBuffer();
		fBuffer.append("DELETE FROM " + tablefname + " WHERE 1=1");
		for (int key = 0; keys != null && keys.size() > 0 && key < keys.size(); key++) {
			fBuffer.append("  AND " + keys.get(key) + "=?");
		}
		try {
			result = SysDao.getCommonServer().doDelete(fBuffer.toString(),
					values, true);
			result = Integer.parseInt(result) > 0 ? "" : "删除失败";
		} catch (Exception e) {
			result = e.getMessage();
		}
		return result;
	}

	public static String doFindEntity(List<String> keys, Object[] values,
			String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT *  FROM " + clazz + " WHERE 1=1");
		stringBuffer.append(" AND " + keys.get(0) + "=?");
		Map<String, Object> entity = SysDao.getCommonServer().doFindEntity(
				stringBuffer.toString(), values[0].toString(), true);
		result = JsonHelper.toJsonString(entity);
		return result;

	}

	public static String initCosttype(String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1 ");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		List<Map<String, Object>> mapLs = new ArrayList<Map<String, Object>>();
		for (int key = 0; maps != null && maps.size() > 0 && key < maps.size(); key++) {
			Map<String, Object> mapLocal = maps.get(key);
			Map<String, Object> map = new HashMap<String, Object>();
			String valuz = mapLocal.get("costtypename").toString();
			map.put("label", valuz);
			map.put("value", valuz);

			mapLs.add(map);
		}
		result = JsonHelper.toJSONString(mapLs);
		return result;
	}

	public static String initCitytype(String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1 ");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		List<Map<String, Object>> mapLs = new ArrayList<Map<String, Object>>();
		for (int key = 0; maps != null && maps.size() > 0 && key < maps.size(); key++) {
			Map<String, Object> mapLocal = maps.get(key);
			Map<String, Object> map = new HashMap<String, Object>();
			String valuz = mapLocal.get("citytypename").toString();
			map.put("label", valuz);
			map.put("value", valuz);

			mapLs.add(map);
		}
		result = JsonHelper.toJSONString(mapLs);
		return result;
	}

	public static String initZone(String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1 ");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		List<Map<String, Object>> mapLs = new ArrayList<Map<String, Object>>();
		for (int key = 0; maps != null && maps.size() > 0 && key < maps.size(); key++) {
			Map<String, Object> mapLocal = maps.get(key);
			Map<String, Object> map = new HashMap<String, Object>();
			String valuz = mapLocal.get("zonename").toString();
			map.put("label", valuz);
			map.put("value", valuz);

			mapLs.add(map);
		}
		result = JsonHelper.toJSONString(mapLs);
		return result;
	}

	public static String initZoneitem(String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1 ");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		List<Map<String, Object>> mapLs = new ArrayList<Map<String, Object>>();
		for (int key = 0; maps != null && maps.size() > 0 && key < maps.size(); key++) {
			Map<String, Object> mapLocal = maps.get(key);
			Map<String, Object> map = new HashMap<String, Object>();
			String valuz = mapLocal.get("zoneitemname").toString();
			map.put("label", valuz);
			map.put("value", valuz);

			mapLs.add(map);
		}
		result = JsonHelper.toJSONString(mapLs);
		return result;
	}

	public static String initSkuType(String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1 ");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		List<Map<String, Object>> mapLs = new ArrayList<Map<String, Object>>();
		for (int key = 0; maps != null && maps.size() > 0 && key < maps.size(); key++) {
			Map<String, Object> mapLocal = maps.get(key);
			Map<String, Object> map = new HashMap<String, Object>();
			String keyid = mapLocal.get("keyid").toString();
			String valuz = mapLocal.get("skutype").toString();
			map.put("label", valuz);
			map.put("value", keyid);

			mapLs.add(map);
		}
		result = JsonHelper.toJSONString(mapLs);
		return result;

	}

	public static String initBrandName(String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1 ");
		stringBuffer.append(" AND stateid = 'T' ");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		List<Map<String, Object>> mapLs = new ArrayList<Map<String, Object>>();
		for (int key = 0; maps != null && maps.size() > 0 && key < maps.size(); key++) {
			Map<String, Object> mapLocal = maps.get(key);
			Map<String, Object> map = new HashMap<String, Object>();
			String valuz = mapLocal.get("brandname").toString();
			String keyid = mapLocal.get("keyid").toString();
			map.put("label", valuz);
			map.put("value", keyid);

			mapLs.add(map);
		}
		result = JsonHelper.toJSONString(mapLs);
		return result;

	}

	public static String initEunit(String param, String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1 ");
		stringBuffer.append(" AND typelist like '%" + param + "%' ");
		stringBuffer.append(" AND stateid like 'T' ");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		List<Map<String, Object>> mapLs = new ArrayList<Map<String, Object>>();
		for (int key = 0; maps != null && maps.size() > 0 && key < maps.size(); key++) {
			Map<String, Object> mapLocal = maps.get(key);
			Map<String, Object> map = new HashMap<String, Object>();
			String label = mapLocal.get("unitname").toString();
			String valuz = mapLocal.get("keyid").toString();
			map.put("label", label);
			map.put("value", valuz);
			mapLs.add(map);
		}
		result = JsonHelper.toJSONString(mapLs);
		return result;
	}

	public static String initCustomer(String clazz) {

		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1 ");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		List<Map<String, Object>> mapLs = new ArrayList<Map<String, Object>>();
		for (int key = 0; maps != null && maps.size() > 0 && key < maps.size(); key++) {
			Map<String, Object> mapLocal = maps.get(key);
			Map<String, Object> map = new HashMap<String, Object>();
			String label = mapLocal.get("cusnamecn").toString();
			String valuz = mapLocal.get("keyid").toString();
			map.put("label", label);
			map.put("value", valuz);
			mapLs.add(map);
		}
		result = JsonHelper.toJSONString(mapLs);
		return result;
	}

	public static String initTree(List<String> keys, Object[] values,
			String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1");

		for (int key = 0; keys != null && keys.size() > 0 && key < keys.size(); key++) {
			if (StringUtils.isNotBlank(values[key].toString())) {
				stringBuffer.append(" AND " + keys.get(key) + "='"
						+ values[key] + "' ");

			} else {
				stringBuffer.append(" AND " + keys.get(key) + " is null ");
			}

		}
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);
		result = JsonHelper.toJSONString(maps);
		return result;
	}
}
